package gold.digger;

/**
 * Created by fanzhenyu02 on 2020/3/1.
 */
public class LC198 {

    public int rob(int[] arr) {
        if (arr.length <= 0) {
            return 0;
        }

        if (arr.length <= 1) {
            return arr[0];
        }

        int fir = arr[0];
        int sec = arr[1] > arr[0] ? arr[1] : arr[0], cur, res = Math.max(fir, sec);
        for (int i = 2; i < arr.length; i++) {
            cur = Math.max(arr[i] + fir, sec);
            res = Math.max(cur, res);
            fir = sec;
            sec = res;
        }

        return res;
    }

    public static void main(String[] args) {
        LC198 an = new LC198();
        int[] arr = {2, 7, 9, 3, 1};
        System.out.println(an.rob(arr));
    }
}
